package com.smartrobot.smartrobot.websocket;

import android.content.Context;

import android.util.Log;
import com.smartrobot.smartrobot.Common.Common;
import com.smartrobot.smartrobot.logToSdCard.LogToSd;
import com.smartrobot.smartrobot.view.ToastCustom;

import java.util.Date;

/**
 * Created by yeluojun on 2015/1/7.
 */
public class heatBeat {



    private Thread mThread;
    public  static boolean alive = false;
    private static Context context;
    private static long  sendTime = 0;
    private static long timeout = 20000;
    private static int  ping_sleep_time = 3500;
    private static boolean breakHeart =  false;
//    "{\"type\": \"1\"}";
    public final static String ping_str ="";

    public heatBeat(Context context){
        this.context = context;
    }


    //
    public void ping(){
        if (mThread != null && mThread.isAlive()) {
            return;
        }
        mThread = new Thread(new Runnable() {

            @Override
            public void run() {
                while(true){
                    try{

                        Log.d("====心跳开始=====","ping !!!!!!!");




                        /* 判断是否开启心跳 */
                        if(!Common.startHeartBeat){
                            break;
                        }

                        if(breakHeart){
                            alive = false;
                            break;
                        }


//                        Log.e("============breakHeart======",breakHeart+"");
//                        if(!breakHeart){
//                            alive = false;
//                            break;
//                        }
//                        Log.e("============breakHeart222======",breakHeart+"");


                        if(Common.my_webSocketClient==null){
                            socketUtil.initSocket_2(context);  // 初始化一个webSocket连接
                        }

                        Common.my_webSocketClient.connect(); //为了确保已经连接就添加这个

                        Common.my_webSocketClient.ping(ping_str);

                        Date date = new Date();
                        sendTime = date.getTime();

                        Thread.sleep(ping_sleep_time);

                        //                        测试用，要删除
//                        if(!breakHeart){
//                            break;
//                        }


                    }catch (Exception e){
                        Log.e("心跳异常", e.toString());
                        LogToSd.log_init();
                        LogToSd.error(e.toString());
                    }
                }
            }
        });

        mThread.start();

    }




    public static void onPong(){
            Log.d("====websocket=====","pong ! ! ! !");
            alive = true;
            connectTimeout();
    }

    public  static void  setBreakHeart(Boolean b){
        breakHeart = b;
    }


    private static boolean connectTimeout(){
        Date date = new Date();

        if (date.getTime() - sendTime > timeout) {
            ToastCustom.showToast(context, "----网络环境较差----");
            return true;
        }
//        Log.e("=====时间====",date.getTime()+"");
        return false;
    }

}
